package org.wattdepot.cli.command;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.CommandLineInterface;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Displays the total amount of carbon or energy generated (or energy consumed) by a power source
 * for a given day. The sampling interval indicates how to generate the curve of carbon or energy
 * data points.
 * 
 * @author BJ Peter DeLaCruz, Wahib Hanani, Lyneth Peou
 * @version 2.0
 */
public class TotalCarbonOrEnergy extends CliCommandInterface {

  /**
   * Carries out the command inputted by the user on the command prompt.
   * 
   * @param cli A CommandLineInterface object.
   * @param commandList Command inputted by the user on the command prompt.
   * @return Result after running the command.
   */
  public String doCommand(CommandLineInterface cli, List<String> commandList) {
    return listTotal(cli, commandList);
  }

  /**
   * Displays the total amount of carbon or energy generated (or energy consumed) by a power source
   * for a given day. The sampling interval indicates how to generate the curve of carbon or energy
   * data points.
   * 
   * @param cli A CommandLineInterface object.
   * @param command Command inputted by the user on the command prompt.
   * @return Total amount of carbon or energy generated by a power source.
   */
  private final String listTotal(CommandLineInterface cli, List<String> command) {
    XMLGregorianCalendar startTime = null;
    try {
      startTime = Tstamp.makeTimestamp(command.get(5));
    }
    catch (Exception e) {
      if (cli.debuggingMode) {
        cli.errorEncountered = true;
      } // end if
      return ERROR_MESSAGE + STRING_CONVERSION_ERROR;
    } // end catch
    XMLGregorianCalendar endTime = Tstamp.incrementDays(startTime, 1);
    endTime = Tstamp.incrementSeconds(endTime, -1);

    String result = "";
    try {
      if (command.get(1).equalsIgnoreCase("carbon")) {
        if (command.get(2).equalsIgnoreCase(CONSUMED_COMMAND)) {
          result = "No data for the total amount of carbon consumed is available at the moment.";
        } // end if
        else if (command.get(2).equalsIgnoreCase(GENERATED_COMMAND)) {
          double carbonGenerated =
              cli.CLIENT.getCarbonEmitted(command.get(3), startTime, endTime, Integer
                  .parseInt(command.get(7)));
          result = "Carbon Generated: " + ONE_DECIMAL_PLACE.format(carbonGenerated) + " lbs CO2";
        } // end else if
      } // end if
      else if (command.get(1).equalsIgnoreCase("energy")) {
        if (command.get(2).equalsIgnoreCase(GENERATED_COMMAND)) {
          double energyGenerated =
              cli.CLIENT.getEnergyGenerated(command.get(3), startTime, endTime, Integer
                  .parseInt(command.get(7)));
          if (Double.compare(energyGenerated, 0.0) == 0) {
            result =
                "Data regarding total energy generated for " + command.get(3) + " is unavailable.";
          } // end if
          else {
            result = "Energy Generated: " + ONE_DECIMAL_PLACE.format(energyGenerated) + " Wh";
          } // end else
        } // end if
        else if (command.get(2).equalsIgnoreCase(CONSUMED_COMMAND)) {
          double energyConsumed =
              cli.CLIENT.getEnergyConsumed(command.get(3), startTime, endTime, Integer
                  .parseInt(command.get(7)));
          if (Double.compare(energyConsumed, 0.0) == 0) {
            result =
                "Data regarding total energy consumed for " + command.get(3) + " is unavailable.";
          } // end if
          else {
            result = "Energy Consumed: " + ONE_DECIMAL_PLACE.format(energyConsumed) + " Wh";
          } // end else
        } // end else if
      } // end else if
    } // end try
    catch (NumberFormatException nfe) {
      if (cli.debuggingMode) {
        cli.errorEncountered = true;
      } // end if
      result = ERROR_MESSAGE + "Number for sampling interval is in wrong format. Please try again.";
    } // end catch
    catch (ResourceNotFoundException rnfe) {
      if (cli.debuggingMode) {
        cli.errorEncountered = true;
      } // end if
      result = ERROR_MESSAGE + "Power source not found.";
    } // end catch
    catch (Exception e) {
      cli.errorEncountered = true;
      result = ERROR_MESSAGE + CARRIAGE_RETURN + e.toString() + CARRIAGE_RETURN + EXIT_MESSAGE;
    }

    return result;
  }

}